﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace PWDepositary
{
    public partial class MainForm : Form
    {
        private List<Memory> _data = new List<Memory>();
        List<int> _index = new List<int>();
        private bool _passview;

        private CustomBorder _lBoxTagBorder;
        private CustomBorder _lBoxNameBorder;
        public MainForm()
        {
            Cursor.Current = Cursors.WaitCursor;
            InitializeComponent();
            if (File.Exists(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)
             + "\\" + Database.DatabaseName))
            {
                var f3 = new Form3();
                f3.ShowDialog();
            }
            else
            {
                var f2 = new Form2();
                f2.ShowDialog();
            }
            InitializeMemory();
            Cursor.Current = Cursors.Default;
            _lBoxTagBorder = new CustomBorder(lBoxTag) { BorderColor = Color.SteelBlue, RoundSize = new Size(5, 5) };
            _lBoxNameBorder = new CustomBorder(lBoxName) { BorderColor = Color.SteelBlue, RoundSize = new Size(5, 5) };
        }

        public void AddPass (int id,string name, string tag, string details, string pass)
        {
            if (lBoxTag.Items.Contains(tag)==false)
            lBoxTag.Items.Add(tag);
            _data.Add(new Memory(id ,name, tag, details, pass));
        }

        public void UpDateForm(int id, string name, string tag, string details, string pass)
        {
            _data[_index[lBoxName.SelectedIndex]].Name = name;
            _data[_index[lBoxName.SelectedIndex]].Tag = tag;
            _data[_index[lBoxName.SelectedIndex]].Details = details;
            _data[_index[lBoxName.SelectedIndex]].Pass = pass;
            lBoxTag.SelectedIndex = -1;
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuItemAddPW_Click(object sender, EventArgs e)
        {
            var fAdd = new FormAdd (lBoxTag.Items) {Owner = this};
            fAdd.ShowDialog();
            lBoxTag_SelectedIndexChanged(sender, e);
        }

        private void lBoxTag_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (lBoxTag.SelectedIndex != -1)
            {
                if (_passview)
                {
                    menuItemView.Text = "Показать";
                    _passview = false;
                }
                lBoxName.Items.Clear();
                _index.Clear();
                lblPWDb.Text = "";
                lkLblDetails.Text = "";
                string curItem = lBoxTag.SelectedItem.ToString();
                int i = 0;
                foreach (Memory s in _data)
                {
                    if (s.Tag == curItem)
                    {
                        lBoxName.Items.Add(s.Name);
                        _index.Add(i);
                    }
                    i++;
                }
            }
            else
            {
                lBoxName.Items.Clear();
            }
        }

        private void lBoxName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lBoxName.SelectedIndex != -1)
            {
                lkLblDetails.Text = _data[_index[lBoxName.SelectedIndex]].Details;
                lblPWDb.Text = "*****";
                if (_passview)
                {
                    menuItemView.Text = "Показать";
                    _passview = false;
                }
            }
        }

        void lkLblDetails_Click(object sender, EventArgs e)
        {
            if (lkLblDetails.Text != "")
            {
                MessageBox.Show(lkLblDetails.Text = _data[_index[lBoxName.SelectedIndex]].Details, "Детали",
                                MessageBoxButtons.OKCancel, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
            }
        }

        private void menuItemView_Click(object sender, EventArgs e)
        {
  
            if (lBoxName.SelectedIndex != -1)
            {
                if (_passview == false)
                {
                    lblPWDb.Text = _data[_index[lBoxName.SelectedIndex]].Pass;
                    menuItemView.Text = "Скрыть";
                    _passview = true;
                }
                else
                {
                    menuItemView.Text = "Показать";
                    lblPWDb.Text = "*****";
                    _passview = false;
                }
             }
        }

        public void InitializeMemory()
        {
            IDataReader dataReader = Database.GetListPassword();
            if (dataReader != null)
                while (dataReader.Read())
                {
                    AddPass(dataReader.GetInt32(0), dataReader.GetString(1),
                            dataReader.GetString(2), dataReader.GetString(3), dataReader.GetString(4));
                }
        }

        private void menuItemAdd_Click(object sender, EventArgs e)
        {
            FormAdd fAdd = lBoxTag.SelectedIndex != -1 ? new FormAdd(lBoxTag.SelectedItem.ToString(), lBoxTag.Items) : new FormAdd(lBoxTag.Items);
            fAdd.Owner = this;
            fAdd.ShowDialog();
            lBoxTag_SelectedIndexChanged(sender, e);
        }

        private void menuItemUpdate_Click(object sender, EventArgs e)
        {
            if (lBoxTag.SelectedIndex != -1)
            {
                var fAdd = new FormAdd(_data[_index[lBoxName.SelectedIndex]].ID, _data[_index[lBoxName.SelectedIndex]].Name,
                                           _data[_index[lBoxName.SelectedIndex]].Tag, _data[_index[lBoxName.SelectedIndex]].Details,
                                           _data[_index[lBoxName.SelectedIndex]].Pass, lBoxTag.Items) { Owner = this };
                fAdd.ShowDialog();
            }
        }

        private void menuItemDelete_Click(object sender, EventArgs e)
        {
            if (lBoxName.SelectedIndex != -1)
            {
                Database.DeleteFromDatabase(_data[_index[lBoxName.SelectedIndex]].ID);
                _data.RemoveAt(_index[lBoxName.SelectedIndex]);
                _index.RemoveAt(lBoxName.SelectedIndex);
                lBoxName.Items.RemoveAt(lBoxName.SelectedIndex);
                if (lBoxName.Items.Count == 0)
                    lBoxTag.Items.RemoveAt(lBoxTag.SelectedIndex);
                lBoxTag_SelectedIndexChanged(sender, e);
            }
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            var assem = Assembly.GetExecutingAssembly().GetName();
            MessageBox.Show(assem.Name + " v" + assem.Version.Major + "." + assem.Version.Minor
                + "\n\n © 2009 W&B Soft Mobile", "О программе"
            , MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
        }

        [DllImport("coredll.dll")]
        static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
        private const int SW_MINIMIZED = 6;

        private void menuItemMinim_Click(object sender, EventArgs e)
        {
            ShowWindow(Handle, SW_MINIMIZED);
        }

        private void menuItemChangePW_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(1);
            f2.ShowDialog();
        }
    }
}